<template>
  <div class="app-container">
    <div>
      <BasicBusiness ref="basicBusinessRef" />
    </div>
    <div style="padding: 50px">
      <Step ref="stepRef" />
    </div>
    <el-row :gutter="15">
      <el-form
        ref="elForm"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="180px"
      >
        <el-form-item
          label="后项采购技术规格书"
          prop="afterBuyTechnicalFileId"
          required
        >
          <el-upload
            ref="afterBuyTechnicalFileId"
            :file-list="afterBuyTechnicalFileIdfileList"
            :action="action"
            :before-upload="beforeUpload"
            :limit="1"
            :on-success="handleUploadSuccess"
            :on-remove="handleRemove"
            :headers="headers"
          >
            <el-button size="small" type="primary" icon="el-icon-upload"
              >点击上传</el-button
            >
          </el-upload>
        </el-form-item>
        <el-form-item label="拟稿合同" prop="draftContractId" required>
          <el-upload
            ref="draftContractId"
            :file-list="draftContractIdfileList"
            :action="action"
            :before-upload="beforeUpload"
            :limit="1"
            :on-success="handleUploadSuccess2"
            :on-remove="handleRemove2"
            :headers="headers"
          >
            <el-button size="small" type="primary" icon="el-icon-upload"
              >点击上传</el-button
            >
          </el-upload>
        </el-form-item>
        <el-form-item size="mini">
          <el-button type="primary" @click="submitForm">提交</el-button>
        </el-form-item>
      </el-form>
    </el-row>
  </div>
</template>
<script>
import BasicBusiness from "@/views/components/business/BasicBusiness.vue";
import Step from "@/views/components/business/Step.vue";

import { getProjectDetail, draftContract } from "@/api/project/index";
import { getToken } from "@/utils/auth";
export default {
  components: { BasicBusiness, Step },
  props: [],
  data() {
    return {
      formData: {
        afterBuyTechnicalFileId: null,
        draftContractId: null,
      },
      rules: {},
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      action:
        "http://business.byteturing.com" +
        process.env.VUE_APP_BASE_API +
        "/common/upload",
      draftContractIdfileList: [],
      afterBuyTechnicalFileIdfileList: [],
    };
  },
  computed: {
    boId() {
      return this.$route.query.boId;
    },
    projectId() {
      return this.$route.query.projectId;
    },
  },
  watch: {},
  created() {},
  mounted() {
    this.loadData();
  },
  methods: {
    loadData() {
      this.$refs.basicBusinessRef.loadData(this.boId).then((res) => {});

      getProjectDetail(this.boId).then((res) => {
        this.stage = res.data.stage;
        this.$refs.stepRef.setStage(this.stage);
      });
    },
    submitForm() {
      this.$refs["elForm"].validate((valid) => {
        if (!valid) return;
        // TODO 提交表单
        draftContract({
          ...this.formData,
          boId: this.boId,
          projectId: this.projectId,
        }).then((res) => {
          this.$message({
            message: "操作成功",
            type: "success",
          });
          this.$router.replace('/contract/index');
        });
      });
    },
    resetForm() {
      this.$refs["elForm"].resetFields();
    },
    beforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 100;
      if (!isRightSize) {
        this.$message.error("文件大小超过 100MB");
      }
      return isRightSize;
    },
    handleUploadSuccess(res, file) {
      this.formData.afterBuyTechnicalFileId = res.fileId;
    },
    handleRemove() {
      this.formData.afterBuyTechnicalFileId = "";
    },

    handleUploadSuccess2(res, file) {
      this.formData.draftContractId = res.fileId;
    },
    handleRemove2() {
      this.formData.draftContractId = "";
    },
  },
};
</script>
<style></style>
